﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace HSIA
{
    public class TextAndImageColumn : DataGridViewTextBoxColumn
    {
        private Image imageValue;
        private Size imageSize;

        public TextAndImageColumn()
        {
            this.CellTemplate = new TextAndImageCell();
        }

        public override object Clone()
        {
            TextAndImageColumn c = base.Clone() as TextAndImageColumn;
            c.imageValue = this.imageValue;
            c.imageSize = this.imageSize;
            return c;
        }
        public Image Image
        {
            get { return this.imageValue; }
            set
            {
                if (value != null && this.Image != value)
                {
                    this.imageValue = value;
                    this.imageSize = value.Size;

                    if (this.InheritedStyle != null)
                    {
                        Padding inheritedPadding = this.InheritedStyle.Padding;
                        this.DefaultCellStyle.Padding = new Padding(imageSize.Width,
                        inheritedPadding.Top, inheritedPadding.Right,
                        inheritedPadding.Bottom);
                    }
                }
            }
        }
        private TextAndImageCell TextAndImageCellTemplate
        {
            get { return this.CellTemplate as TextAndImageCell; }
        }
        internal Size ImageSize
        {
            get { return imageSize; }
        }
    }

    public class TextAndImageCell : DataGridViewTextBoxCell
    {
        private Image imageValue;
        private Size imageSize;

        public override object Clone()
        {
            TextAndImageCell c = base.Clone() as TextAndImageCell;
            c.imageValue = this.imageValue;
            c.imageSize = this.imageSize;
            return c;
        }

        public Image Image
        {
            get
            {
                if (this.OwningColumn == null || this.OwningTextAndImageColumn == null)
                {
                    return imageValue;
                }
                else if (this.imageValue != null)
                {
                    return this.imageValue;
                }
                else
                {
                    return this.OwningTextAndImageColumn.Image;
                }
            }
            set
            {
                if (value != null && this.imageValue != value)
                {
                    this.imageValue = value;
                    this.imageSize = value.Size;

                    //Padding inheritedPadding = this.InheritedStyle.Padding;
                    //this.Style.Padding = new Padding(imageSize.Width,
                    //inheritedPadding.Top, inheritedPadding.Right,
                    //inheritedPadding.Bottom);
                }
            }
        }

        protected override void Paint(Graphics graphics, Rectangle clipBounds,
                                                                 Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState,
                                                                 object value, object formattedValue, string errorText,
                                                                 DataGridViewCellStyle cellStyle,
                                                                 DataGridViewAdvancedBorderStyle advancedBorderStyle,
                                                                 DataGridViewPaintParts paintParts)
        {
            // Paint the base content 
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
                       value, formattedValue, errorText, cellStyle,
                       advancedBorderStyle, paintParts);

            if (this.Image != null)
            {
                // Draw the image clipped to the cell. 
                System.Drawing.Drawing2D.GraphicsContainer container = graphics.BeginContainer();
                graphics.SetClip(cellBounds);
                Point pLocation = new Point(cellBounds.Location.X + 3, cellBounds.Location.Y + 3);
                graphics.DrawImageUnscaled(this.Image, pLocation);//cellBounds.Location
                graphics.EndContainer(container);
            }
        }

        private TextAndImageColumn OwningTextAndImageColumn
        {
            get { return this.OwningColumn as TextAndImageColumn; }
        }
    }

    public class MyUpdate
    {
        public static string SetCrypto(string str, string sKey)
        {
            try
            {
                if ((str == "") || (sKey == "")) return "";
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.Default.GetBytes(str);
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                StringBuilder ret = new StringBuilder();
                foreach (byte b in ms.ToArray())
                {
                    //Format  as  hex  
                    ret.AppendFormat("{0:X2}", b);
                }
                ret.ToString();
                return ret.ToString();
            }
            catch
            {
                return "KO";
            }
        }

        public static void AutoUpdate(Socket sClient, DatabaseObject dbo, string strFullPath)
        {
            string sIP = "127.0.0.1";
            try
            {
                IPEndPoint ip = sClient.RemoteEndPoint as IPEndPoint;
                sIP = ip.Address.ToString();
            }
            catch
            {
            }

            string AutoUpdateExe = Application.StartupPath + "\\" + "AmateUpdate.exe";
            try
            {
                string sInfo = AmateBussiness.GetSystemParamValue(dbo, "CompanyName_" + PublicFunc.sLanguage, "阿米特").ToString() +
                      " /r/n " + AmateBussiness.GetSystemParamValue(dbo, "EMail", "").ToString() +
                      " /r/n " + AmateBussiness.GetSystemParamValue(dbo, "Web ", "").ToString() +
                      " /r/n " + AmateBussiness.GetSystemParamValue(dbo, "Phone", "").ToString() +
                      " /r/n " + AmateBussiness.GetSystemParamValue(dbo, "ProductName_" + PublicFunc.sLanguage, "").ToString() +
                      " /r/n " + AmateBussiness.GetSystemParamValue(dbo, "Version ", "").ToString();

                ProcessStartInfo startInfo = new ProcessStartInfo(AutoUpdateExe);
                string cmdLine = null;
                string strFileName = Path.GetFileName(strFullPath);
                cmdLine = strFileName + "|" + sIP + "|" + sInfo;
                startInfo.Arguments = cmdLine;
                Process.Start(startInfo);
                if (sClient != null) sClient.Close();
            }
            catch //(Exception ex)
            {
                //MessageBox.Show("启动自动升级检查程序失败" + "\r\n" + AutoUpdateExe + ":\r\n" + ex.Message, Text);
            }
        }

        public static void AutoUpdate(string sIP, string sPort, DatabaseObject dbo, string strFullPath)
        {
            string AutoUpdateExe = Application.StartupPath + "\\" + "AmateUpdate.exe";
            try
            {
                string sInfo = AmateBussiness.GetSystemParamValue(dbo, "CompanyName_" + PublicFunc.sLanguage, "阿米特").ToString() +
                    //"\r\n " + AmateBussiness.GetSystemParamValue(dbo, dbo.dtSystemParam, "EMail", "").ToString() +
                    //"\r\n" + AmateBussiness.GetSystemParamValue(dbo, dbo.dtSystemParam, "Web ", "").ToString() +
                    //"\r\n" + AmateBussiness.GetSystemParamValue(dbo, dbo.dtSystemParam, "Phone", "").ToString() +
                    //"\r\n" + AmateBussiness.GetSystemParamValue(dbo, dbo.dtSystemParam, "ProductName_" + PublicFunc.sLanguage, "").ToString() +
                      "\r\n" + AmateBussiness.GetSystemParamValue(dbo, "Version ", "").ToString();

                ProcessStartInfo startInfo = new ProcessStartInfo(AutoUpdateExe);
                string cmdLine = null;
                string strFileName = Path.GetFileName(strFullPath);
                cmdLine = strFileName + "|" + sIP + "|" + sPort + "|" + sInfo;
                startInfo.Arguments = cmdLine;
                Process.Start(startInfo);
            }
            catch //(Exception ex)
            {
                //MessageBox.Show("启动自动升级检查程序失败" + "\r\n" + AutoUpdateExe + ":\r\n" + ex.Message, Text);
            }
        }

    }

}
